package com.xintonghua.bussiness.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.xintonghua.base.widget.MyItemTextView;
import com.xintonghua.bussiness.BuildConfig;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.AuthInfoBean;
import com.xintonghua.bussiness.bean.CustomerBean;
import com.xintonghua.bussiness.bean.StaffBean;
import com.xintonghua.bussiness.bean.UserInfoBean;
import com.xintonghua.bussiness.ui.user.manage.PositionActivity;
import com.xintonghua.bussiness.util.GlideUtils;
import com.xintonghua.bussiness.util.ImageTools;
import com.xintonghua.bussiness.util.MyState;
import com.xintonghua.bussiness.util.MyUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.addapp.pickers.listeners.OnItemPickListener;
import cn.addapp.pickers.picker.DatePicker;
import cn.addapp.pickers.picker.SinglePicker;
import cn.addapp.pickers.widget.WheelView;
import me.iwf.photopicker.PhotoPicker;

/**
 * 编辑会员资料
 */
public class ModifyMemberActivity extends BaseActivity {

    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.edt_username)
    EditText edtUsername;
    @BindView(R.id.utils_sex)
    MyItemTextView utilsSex;
    @BindView(R.id.edt_wx)
    EditText edtWx;
    @BindView(R.id.edt_blood_type)
    EditText edtBloodType;
    @BindView(R.id.edt_height)
    EditText edtHeight;
    @BindView(R.id.edt_weight)
    EditText edtWeight;
    @BindView(R.id.edt_period)
    EditText edtPeriod;
    @BindView(R.id.edt_speciality)
    EditText edtSpeciality;
    @BindView(R.id.edt_address)
    EditText edtAddress;
    @BindView(R.id.edt_email)
    EditText edtEmail;
    @BindView(R.id.edt_member_number)
    EditText edtMemberNumber;
    @BindView(R.id.edt_introduce)
    EditText edtIntroduce;
    @BindView(R.id.edt_principal)
    EditText edtPrincipal;
    @BindView(R.id.edt_recommend)
    EditText edtRecommend;
    @BindView(R.id.item_shoplifting)
    MyItemTextView itemShoplifting;
    @BindView(R.id.edt_channel)
    EditText edtChannel;
    @BindView(R.id.edt_remark)
    EditText edtRemark;
    @BindView(R.id.item_constellation)
    MyItemTextView itemConstellation;
    @BindView(R.id.item_birthday)
    MyItemTextView itemBirthday;
    @BindView(R.id.edt_type)
    EditText edtType;
    private List<String> pList = new ArrayList<>();
    private int sex = 0;
    private String imagePath;

    UserInfoBean userInfoBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_member);
        ButterKnife.bind(this);
        onSimpleActionBar();
        userInfoBean = getIntent().getParcelableExtra(BuildConfig.FLAVOR);
        setSimpleActionBar("编辑会员资料", "保存", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                save();
            }
        });
        httpCent.getCustomsInfo(userInfoBean.getId(), userInfoBean.getShopId(),
                this, MyState.requestOne);
        initUI();

    }

    @Override
    public void initUI() {
        super.initUI();
        pList.add("男");
        pList.add("女");
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne:
                CustomerBean bean = JSONObject.parseObject(data.toString(), CustomerBean.class);
                if (bean != null) {
                    imagePath = bean.getHead_img();
                    sex = bean.getSex();
                    GlideUtils.loadCricle(this, ivHead, bean.getHead_img(), R.drawable.default_head);
                    edtUsername.setText(bean.getNickname());
                    utilsSex.setMsg(bean.getSex() == 0 ? "女" : "男");
                    edtWx.setText(bean.getWx());
                    itemConstellation.setMsg(bean.getConstellation());
                    edtType.setText(bean.getMemberType());
                    edtBloodType.setText(bean.getBloodType());
                    edtHeight.setText(bean.getHeight());
                    edtWeight.setText(bean.getWeight());
                    edtPeriod.setText(bean.getPremenstrualCondition());
                    edtSpeciality.setText(bean.getOccupation());
                    edtAddress.setText(bean.getStoreAddress());
                    edtEmail.setText(bean.getEmail());
                    edtMemberNumber.setText(bean.getMembershipNumber());
                    edtIntroduce.setText(bean.getIntroducer());
                    edtPrincipal.setText(bean.getHairdresser());
                    edtRecommend.setText(bean.getReferee());
                    itemShoplifting.setMsg(bean.getEntryTime());
                    edtChannel.setText(bean.getSource());
                    edtRemark.setText(bean.getCustomerNote());
                    itemBirthday.setMsg(bean.getBirthday());
                }
                break;
            case MyState.requestTwo:
                imagePath = (String) data;
                GlideUtils.loadCricle(this, ivHead, imagePath);
                break;
            case MyState.requestThree:
                mToast("保存成功！");
                break;
        }
    }

    /**
     * 保存用户信息
     */
    private void save() {
        if (TextUtils.isEmpty(imagePath)) {
            mToast("上传头像不能为空！");
            return;
        }
        String Introducer = getStr(edtIntroduce);
        String constellation = itemConstellation.getMsg();
        String email = getStr(edtEmail);
        String entryTime = itemShoplifting.getMsg();
        String hairdresser = getStr(edtPrincipal);
        String height = getStr(edtHeight);
        String memberType = getStr(edtType);
        String membershipNumber = getStr(edtMemberNumber);
        String nickname = getStr(edtUsername);
        String note = getStr(edtRemark);
        String occupation = getStr(edtSpeciality);
        String premenstrualCondition = getStr(edtPeriod);
        String referee = getStr(edtRecommend);
        String source = getStr(edtChannel);
        String storeAddress = getStr(edtAddress);
        String weight = getStr(edtHeight);
        String wx = getStr(edtWx);
        httpCent.saveMemberDetailInfo(Introducer, constellation, email, entryTime, hairdresser, imagePath, height,
                memberType, membershipNumber, nickname, note, occupation, premenstrualCondition,
                referee, sex, userInfoBean.getShopId(), source, storeAddress, userInfoBean.getId(),
                weight, wx, itemBirthday.getMsg(), this, MyState.requestThree);

    }

    @OnClick({R.id.iv_head, R.id.utils_sex, R.id.item_constellation, R.id.item_shoplifting})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_head:
                MyUtils.startPhotoPicker(this, 1, 100);
                break;
            case R.id.utils_sex:
                showSex();
                break;
            case R.id.item_constellation:
                constellation();
                break;
            case R.id.item_shoplifting:
                onYearMonthDayPicker(view);
                break;
        }
    }

    /**
     * 选择星座
     */
    private void constellation() {
        String[] constellation = new String[]{
                "水瓶", "双鱼", "白羊", "金牛", "双子", "巨蟹",
                "狮子", "处女", "天秤", "天蝎", "射手", "摩羯"};
        SinglePicker<String> picker = new SinglePicker<>(this, Arrays.asList(constellation));
        picker.setTopHeight(50);//顶部标题栏高度
        picker.setTopLineColor(0xFF33B5E5);//顶部标题栏下划线颜色
        picker.setTopLineHeight(1);//顶部标题栏下划线高度
        picker.setTitleText("请选择");
        picker.setTitleTextColor(0xFF999999);//顶部标题颜色
        picker.setTitleTextSize(12);//顶部标题文字大小
        picker.setCancelTextColor(0xFF33B5E5);//顶部取消按钮文字颜色
        picker.setCancelTextSize(14);
        picker.setSubmitTextColor(0xFF33B5E5);//顶部确定按钮文字颜色
        picker.setSubmitTextSize(14);
        picker.setBackgroundColor(0xFFE1E1E1);
        picker.setSelectedIndex(10);//默认选中项
        picker.setOnItemPickListener(new OnItemPickListener<String>() {
            @Override
            public void onItemPicked(int index, String item) {
                itemConstellation.setMsg(item + "座");
            }
        });
    }

    public void onYearMonthDayPicker(View view) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        final DatePicker picker = new DatePicker(this);
        picker.setCanLoop(true);
        picker.setWheelModeEnable(true);
        picker.setTopPadding(15);
        picker.setRangeStart(1790, 8, 29);
        picker.setRangeEnd(2111, 1, 11);
        picker.setSelectedItem(year, month, day);
        picker.setWeightEnable(true);
        picker.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
            @Override
            public void onDatePicked(String year, String month, String day) {
                itemShoplifting.setMsg(year + "-" + month + "-" + day);
            }
        });
        picker.setOnWheelListener(new DatePicker.OnWheelListener() {

            @Override
            public void onYearWheeled(int index, String year) {
                picker.setTitleText(year + "-" + picker.getSelectedMonth() + "-" + picker.getSelectedDay());
            }

            @Override
            public void onMonthWheeled(int index, String month) {
                picker.setTitleText(picker.getSelectedYear() + "-" + month + "-" + picker.getSelectedDay());
            }

            @Override
            public void onDayWheeled(int index, String day) {
                picker.setTitleText(picker.getSelectedYear() + "-" + picker.getSelectedMonth() + "-" + day);
            }
        });
        picker.show();
    }

    private void showSex() {
        SinglePicker<String> picker = new SinglePicker<>(this, pList);
        picker.setCanLoop(false);//不禁用循环
        picker.setLineVisible(true);
        picker.setShadowVisible(true);
        picker.setTextSize(18);
        picker.setSelectedIndex(1);
        picker.setWheelModeEnable(true);
        //启用权重 setWeightWidth 才起作用
        picker.setLabel(null);
        picker.setWeightEnable(true);
        picker.setWeightWidth(1);
        picker.setSelectedTextColor(0xFF279BAA);//前四位值是透明度
        picker.setUnSelectedTextColor(0xFF999999);
        picker.setOnItemPickListener(new OnItemPickListener<String>() {
            @Override
            public void onItemPicked(int index, String item) {
                if (pList.get(index).equals("男")) {
                    sex = 1;
                } else {
                    sex = 0;
                }
                if (sex == 0) {
                    utilsSex.setMsg("女");
                } else {
                    utilsSex.setMsg("男");
                }
            }
        });
        picker.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 100:
                    if (data != null) {
                        ArrayList<String> photos = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
                        if (photos != null) {
                            ImageTools.loadImage(this, photos.get(0), this, MyState.requestTwo);
                        }
                    }
            }
        }
    }
}
